package com.ruoyi.pk.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pk.domain.PkEjDailyReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pk.mapper.PkEjDailyReportMapper;
import com.ruoyi.pk.domain.PkEjDailyReport;
import com.ruoyi.pk.service.IPkEjDailyReportService;

/**
 * 二九日报Service业务层处理
 *
 * @author wang
 * @date 2021-09-13
 */
@Service
public class PkEjDailyReportServiceImpl implements IPkEjDailyReportService
{
    @Autowired
    private PkEjDailyReportMapper pkEjDailyReportMapper;

    /**
     * 查询二九日报
     *
     * @param id 二九日报主键
     * @return 二九日报
     */
    @Override
    public PkEjDailyReport selectPkEjDailyReportById(Long id)
    {
        return pkEjDailyReportMapper.selectPkEjDailyReportById(id);
    }

    /**
     * 查询二九日报列表
     *
     * @param pkEjDailyReport 二九日报
     * @return 二九日报
     */
    @Override
    public List<PkEjDailyReport> selectPkEjDailyReportList(PkEjDailyReport pkEjDailyReport)
    {
        return pkEjDailyReportMapper.selectPkEjDailyReportList(pkEjDailyReport);
    }

    /**
     * 新增二九日报
     *
     * @param pkEjDailyReport 二九日报
     * @return 结果
     */
    @Override
    public int insertPkEjDailyReport(PkEjDailyReport pkEjDailyReport)
    {
        pkEjDailyReport.setPky(SecurityUtils.getUsername());
        return pkEjDailyReportMapper.insertPkEjDailyReport(pkEjDailyReport);
    }

    /**
     * 修改二九日报
     *
     * @param pkEjDailyReport 二九日报
     * @return 结果
     */
    @Override
    public int updatePkEjDailyReport(PkEjDailyReport pkEjDailyReport)
    {
        if(pkEjDailyReport.getSftj().equals("2")){//值长点击审核结束
            pkEjDailyReport.setZz(SecurityUtils.getUsername());
            pkEjDailyReport.setZzTxsj(new Date());
            pkEjDailyReport.setSftj("2");//表示流转完毕

            //其他审核节点数据设置为"-"
            pkEjDailyReport.setCz("-");
            pkEjDailyReport.setCzyj("-");
            pkEjDailyReport.setPkzg("-");
            pkEjDailyReport.setPkzgyj("-");
            pkEjDailyReport.setPkjl("-");
            pkEjDailyReport.setPkjlyj("-");
        }else {
            //记录责任人填写时间
            String roleKey = SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleKey();//查询当前登录者的角色信息
            if(roleKey.equals("ej_zrr")){ //二九责任人
                pkEjDailyReport.setZrrtxsj(new Date());
            }
            //记录值长意见、填写时间
            if (pkEjDailyReport.getZzyj() != null && pkEjDailyReport.getPkzgyj() == null && pkEjDailyReport.getPkjlyj() == null && pkEjDailyReport.getCzyj() == null) {
                pkEjDailyReport.setZzTxsj(new Date());
                pkEjDailyReport.setZz(SecurityUtils.getUsername());
            }
            //记录品控主管意见、填写时间
            if (pkEjDailyReport.getZzyj() != null && pkEjDailyReport.getPkzgyj() != null && pkEjDailyReport.getPkjlyj() == null && pkEjDailyReport.getCzyj() == null) {
                pkEjDailyReport.setPkzgTxsj(new Date());
                pkEjDailyReport.setPkzg(SecurityUtils.getUsername());
            }
            //记录品控经理意见、填写时间
            if (pkEjDailyReport.getZzyj() != null && pkEjDailyReport.getPkjlyj() != null && pkEjDailyReport.getPkzgyj() != null  && pkEjDailyReport.getCzyj() == null) {
                pkEjDailyReport.setPkjlTxsj(new Date());
                pkEjDailyReport.setPkjl(SecurityUtils.getUsername());
            }
            //记录厂长/主任意见、填写时间
            if (pkEjDailyReport.getZzyj() != null && pkEjDailyReport.getPkzgyj() != null && pkEjDailyReport.getPkjlyj() != null && pkEjDailyReport.getCzyj() != null) {
                pkEjDailyReport.setCzTxsj(new Date());
                pkEjDailyReport.setCz(SecurityUtils.getUsername());
                pkEjDailyReport.setSftj("2");//表示流转完毕
            }
        }
        return pkEjDailyReportMapper.updatePkEjDailyReport(pkEjDailyReport);
    }

    /**
     * 批量删除二九日报
     *
     * @param ids 需要删除的二九日报主键
     * @return 结果
     */
    @Override
    public int deletePkEjDailyReportByIds(Long[] ids)
    {
        return pkEjDailyReportMapper.deletePkEjDailyReportByIds(ids);
    }

    /**
     * 删除二九日报信息
     *
     * @param id 二九日报主键
     * @return 结果
     */
    @Override
    public int deletePkEjDailyReportById(Long id)
    {
        return pkEjDailyReportMapper.deletePkEjDailyReportById(id);
    }

    @Override
    public List<PkEjDailyReport> getListByYzcd(PkEjDailyReport pkEjDailyReport) {
        Map<String, Object> params = pkEjDailyReport.getParams();
        String beginTime = params.get("beginTime").toString();//选择的开始时间，默认为本周一
        DateTime szy = DateUtil.offsetDay(DateUtil.parse(beginTime, "yyyy-MM-dd"), -7); //获取上周周一
        DateTime szt = DateUtil.offsetDay(DateUtil.parse(beginTime, "yyyy-MM-dd"), -1); //获取上周周天
        Map<String, Object> sz = new HashMap<>();
        sz.put("beginTime",szy);
        sz.put("endTime",szt);
        PkEjDailyReport szEjDailyReport = new PkEjDailyReport();
        szEjDailyReport.setParams(sz);
        List<PkEjDailyReport> szList = pkEjDailyReportMapper.getListByYzcd(szEjDailyReport);//选择时间段内上一周的数据
        List<PkEjDailyReport> bzList = pkEjDailyReportMapper.getListByYzcd(pkEjDailyReport);//选择时间段内的数据

        //两周的数据
        List<PkEjDailyReport> all = new ArrayList<>();
        all.addAll(szList);
        all.addAll(bzList);
        List<String> collect = all.stream().map(PkEjDailyReport::getBz).distinct().collect(Collectors.toList());//两周的班组别数据,已去重
        List<PkEjDailyReport> list = new ArrayList<>();
        collect.forEach(item -> {
            PkEjDailyReport zl = new PkEjDailyReport();
            zl.setBz(item);
            szList.forEach(it -> {
                if(it.getBz().equals(item)){
                    if(it.getYzcd().equals("A")){
                        zl.setZrr(it.getRm());
                    }
                    if(it.getYzcd().equals("B")){
                        zl.setWtlb(it.getRm());
                    }
                    if(it.getYzcd().equals("C")){
                        zl.setPkjl(it.getRm());
                    }
                    if(it.getYzcd().equals("一般质量事故")){
                        zl.setCz(it.getRm());
                    }
                    if(it.getYzcd().equals("严重质量事故")){
                        zl.setJjcs(it.getRm());
                    }

                }
            });
            bzList.forEach(it -> {
                if(it.getBz().equals(item)){
                    if(it.getYzcd().equals("A")){
                        zl.setPkzg(it.getRm());
                    }
                    if(it.getYzcd().equals("B")){
                        zl.setCzyj(it.getRm());
                    }
                    if(it.getYzcd().equals("C")){
                        zl.setYzcd(it.getRm());
                    }
                    if(it.getYzcd().equals("一般质量事故")){
                        zl.setPky(it.getRm());
                    }
                    if(it.getYzcd().equals("严重质量事故")){
                        zl.setKf(it.getRm());
                    }

                }
            });
            list.add(zl);
        });
        return list;
    }

    @Override
    public List<PkEjDailyReport> getListByWtlb(PkEjDailyReport pkEjDailyReport) {
        Map<String, Object> params = pkEjDailyReport.getParams();
        String beginTime = params.get("beginTime").toString();//选择的开始时间，默认为本周一
        DateTime szy = DateUtil.offsetDay(DateUtil.parse(beginTime, "yyyy-MM-dd"), -7); //获取上周周一
        DateTime szt = DateUtil.offsetDay(DateUtil.parse(beginTime, "yyyy-MM-dd"), -1); //获取上周周天
        Map<String, Object> sz = new HashMap<>();
        sz.put("beginTime",szy);
        sz.put("endTime",szt);
        PkEjDailyReport szEjDailyReport = new PkEjDailyReport();
        szEjDailyReport.setParams(sz);
        List<PkEjDailyReport> szList = pkEjDailyReportMapper.getListByWtlb(szEjDailyReport);//选择时间段内上一周的数据
        List<PkEjDailyReport> bzList = pkEjDailyReportMapper.getListByWtlb(pkEjDailyReport);//选择时间段内的数据

        //两周的数据
        List<PkEjDailyReport> all = new ArrayList<>();
        all.addAll(szList);
        all.addAll(bzList);
        List<String> collect = all.stream().map(PkEjDailyReport::getWtlb).distinct().collect(Collectors.toList());//两周的问题类别数据,已去重
        List<PkEjDailyReport> list = new ArrayList<>();
        collect.forEach(item -> {
            PkEjDailyReport zl = new PkEjDailyReport();
            zl.setWtlb(item);
            szList.forEach(it -> {
                if(it.getWtlb().equals(item)){ zl.setRm(it.getRm());} //上周的次数
            });
            bzList.forEach(it -> {
                if(it.getWtlb().equals(item)){ zl.setZrr(it.getRm());} //选择周的次数
            });
            list.add(zl);
        });
        return list;
    }

    @Override
    public List<PkEjDailyReport> getListByBzSl(PkEjDailyReport pkEjDailyReport) {
        Map<String, Object> params = pkEjDailyReport.getParams();
        String beginTime = params.get("beginTime").toString();//选择的开始时间，默认为本周一
        DateTime szy = DateUtil.offsetDay(DateUtil.parse(beginTime, "yyyy-MM-dd"), -7); //获取上周周一
        DateTime szt = DateUtil.offsetDay(DateUtil.parse(beginTime, "yyyy-MM-dd"), -1); //获取上周周天
        Map<String, Object> sz = new HashMap<>();
        sz.put("beginTime",szy);
        sz.put("endTime",szt);
        PkEjDailyReport szEjDailyReport = new PkEjDailyReport();
        szEjDailyReport.setParams(sz);
        List<PkEjDailyReport> szList = pkEjDailyReportMapper.getListByBzSl(szEjDailyReport);//选择时间段内上一周的数据
        List<PkEjDailyReport> bzList = pkEjDailyReportMapper.getListByBzSl(pkEjDailyReport);//选择时间段内的数据

        //两周的数据
        List<PkEjDailyReport> all = new ArrayList<>();
        all.addAll(szList);
        all.addAll(bzList);
        List<String> collect = all.stream().map(PkEjDailyReport::getBz).distinct().collect(Collectors.toList());//两周的班组数据,已去重
        List<PkEjDailyReport> list = new ArrayList<>();
        collect.forEach(item -> {
            PkEjDailyReport zl = new PkEjDailyReport();
            zl.setBz(item);
            szList.forEach(it -> {
                if(it.getBz().equals(item)){ zl.setRm(it.getRm());} //上周的次数
            });
            bzList.forEach(it -> {
                if(it.getBz().equals(item)){ zl.setZrr(it.getRm());} //选择周的次数
            });
            list.add(zl);
        });
        return list;
    }

    @Override
    public List<PkEjDailyReport> getListByBzY(PkEjDailyReport pkEjDailyReport) {
        Date txrq = pkEjDailyReport.getTxsj(); //当前选择的月份
        Date sy = DateUtils.getPreDate(DateUtil.formatDate(txrq));//上个月
        pkEjDailyReport.setTxsj(txrq);//当前选择的月份
        pkEjDailyReport.setPkjlTxsj(sy);//上个月
        List<PkEjDailyReport> listByBzSl = pkEjDailyReportMapper.getListByBzY(pkEjDailyReport);
        List<PkEjDailyReport> list = getNewList(listByBzSl, txrq, sy, "bzsl");
        return list;
    }

    @Override
    public List<PkEjDailyReport> getListByWtlbY(PkEjDailyReport pkEjDailyReport) {
        Date txrq = pkEjDailyReport.getTxsj(); //当前选择的月份
        Date sy = DateUtils.getPreDate(DateUtil.formatDate(txrq));//上个月
        pkEjDailyReport.setTxsj(txrq);//当前选择的月份
        pkEjDailyReport.setPkjlTxsj(sy);//上个月
        List<PkEjDailyReport> listByWtlb = pkEjDailyReportMapper.getListByWtlbY(pkEjDailyReport);
        List<PkEjDailyReport> list = getNewList(listByWtlb, txrq, sy, "wtlb");
        return list;
    }

    @Override
    public List<PkEjDailyReport> getListByYzcdY(PkEjDailyReport pkEjDailyReport) {
        Date txrq = pkEjDailyReport.getTxsj(); //当前选择的月份
        Date sy = DateUtils.getPreDate(DateUtil.formatDate(txrq));//上个月
        pkEjDailyReport.setTxsj(txrq);//当前选择的月份
        pkEjDailyReport.setPkjlTxsj(sy);//上个月
        List<PkEjDailyReport> listByWtlb = pkEjDailyReportMapper.getListByYzcdY(pkEjDailyReport);
        List<PkEjDailyReport> list = getNewList(listByWtlb, txrq, sy, "bzwtdj");
        return list;
    }

    public static List<PkEjDailyReport> getNewList(List<PkEjDailyReport> listByCon,Date txrq,Date sy,String f){
        System.err.println("-------->"+listByCon);
        List<PkEjDailyReport> selectDate = listByCon.stream().filter(item -> DateUtil.format(item.getTxsj(), "yyyyMM").equals(DateUtil.format(txrq, "yyyyMM"))).collect(Collectors.toList());
        List<PkEjDailyReport> shangyueDate = listByCon.stream().filter(item -> DateUtil.format(item.getTxsj(), "yyyyMM").equals(DateUtil.format(sy, "yyyyMM"))).collect(Collectors.toList());

        List<String> strList;
        List<PkEjDailyReport> list = new ArrayList<>();
        //获取去重后，所有的班组
        if(f.equals("bzsl")){
            strList = listByCon.stream().map(PkEjDailyReport::getBz).distinct().collect(Collectors.toList());
            strList.forEach(item -> {
                PkEjDailyReport ej = new PkEjDailyReport();
                ej.setBz(item);
                shangyueDate.forEach(it -> {
                    if(it.getBz().equals(item)){ ej.setRm(it.getRm());} //上月的次数
                });
                selectDate.forEach(it -> {
                    if(it.getBz().equals(item)){ ej.setRm(it.getRm());} //选择月的次数
                });
                list.add(ej);
            });
        }
        //获取去重后，所有的问题类别
        if(f.equals("wtlb")){
            strList = listByCon.stream().map(PkEjDailyReport::getWtlb).distinct().collect(Collectors.toList());
            strList.forEach(item -> {
                PkEjDailyReport ej = new PkEjDailyReport();
                ej.setWtlb(item);
                shangyueDate.forEach(it -> {
                    if(it.getWtlb().equals(item)){ ej.setRm(it.getRm());} //上月的次数
                });
                selectDate.forEach(it -> {
                    if(it.getWtlb().equals(item)){ ej.setRm(it.getRm());} //选择月的次数
                });
                list.add(ej);
            });
        }
        //获取去重后，所有的班组、问题等级
        List<PkEjDailyReport> bzAndWtdjList = new ArrayList<>();
        if(f.equals("bzwtdj")){
            //bzAndWtdjList = listByCon.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o->o.getBz()+ ";" + o.getWtdj()))), ArrayList::new));
            strList = listByCon.stream().map(PkEjDailyReport::getBz).distinct().collect(Collectors.toList());
            strList.forEach(item -> {
                PkEjDailyReport ej = new PkEjDailyReport();
                ej.setBz(item);
                shangyueDate.forEach(it -> {
                    if(it.getBz().equals(item)){
                        if(it.getYzcd().equals("A")){
                            ej.setZrr(it.getRm());
                        }
                        if(it.getYzcd().equals("B")){
                            ej.setWtlb(it.getRm());
                        }
                        if(it.getYzcd().equals("C")){
                            ej.setPkjl(it.getRm());
                        }
                        if(it.getYzcd().equals("一般质量事故")){
                            ej.setCz(it.getRm());
                        }
                        if(it.getYzcd().equals("严重质量事故")){
                            ej.setJjcs(it.getRm());
                        }

                    }
                });
                selectDate.forEach(it -> {
                    if(it.getBz().equals(item)){
                        if(it.getYzcd().equals("A")){
                            ej.setPkzg(it.getRm());
                        }
                        if(it.getYzcd().equals("B")){
                            ej.setCzyj(it.getRm());
                        }
                        if(it.getYzcd().equals("C")){
                            ej.setYzcd(it.getRm());
                        }
                        if(it.getYzcd().equals("一般质量事故")){
                            ej.setPky(it.getRm());
                        }
                        if(it.getYzcd().equals("严重质量事故")){
                            ej.setKf(it.getRm());
                        }

                    }
                });
                list.add(ej);
            });
        }
        return list;
    }
}
